<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>json-iterator使用 | anyang的博客</title>
    <meta name="description" content="千里之行,始于足下">
    <meta name="generator" content="VuePress 1.3.1">
    <link rel="icon" href="/img/favicon.ico">
  <script charset="utf-8" src="/js/main.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
  <meta name="keywords" content="福小林,奔奔,ourLang,CentOS,JAVA,vue组件,lsdCloud">
  <script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-146891701-1"></script>
  <script>window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-146891701-1');</script>
  <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?56eae8eec590ccaef1d5ff99d766f315";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    
    <link rel="preload" href="/assets/css/0.styles.9fff9873.css" as="style"><link rel="preload" href="/assets/js/app.1bc80adb.js" as="script"><link rel="preload" href="/assets/js/2.7c0608ab.js" as="script"><link rel="preload" href="/assets/js/96.9fd67185.js" as="script"><link rel="prefetch" href="/assets/js/10.81caa2ab.js"><link rel="prefetch" href="/assets/js/100.05bb269d.js"><link rel="prefetch" href="/assets/js/101.3e18b4d6.js"><link rel="prefetch" href="/assets/js/102.c7d7ed9c.js"><link rel="prefetch" href="/assets/js/103.4c771ce7.js"><link rel="prefetch" href="/assets/js/104.a2acd367.js"><link rel="prefetch" href="/assets/js/105.63399350.js"><link rel="prefetch" href="/assets/js/106.8a4744cc.js"><link rel="prefetch" href="/assets/js/107.7739d796.js"><link rel="prefetch" href="/assets/js/108.8008ae33.js"><link rel="prefetch" href="/assets/js/109.01c63898.js"><link rel="prefetch" href="/assets/js/11.7b837560.js"><link rel="prefetch" href="/assets/js/110.ad01aca9.js"><link rel="prefetch" href="/assets/js/111.3d74db70.js"><link rel="prefetch" href="/assets/js/112.67f55c34.js"><link rel="prefetch" href="/assets/js/113.016cd3c0.js"><link rel="prefetch" href="/assets/js/114.860e4b2b.js"><link rel="prefetch" href="/assets/js/115.b2f26258.js"><link rel="prefetch" href="/assets/js/116.8bea67b4.js"><link rel="prefetch" href="/assets/js/117.20682843.js"><link rel="prefetch" href="/assets/js/118.86a22e16.js"><link rel="prefetch" href="/assets/js/119.ad518bea.js"><link rel="prefetch" href="/assets/js/12.8fd78714.js"><link rel="prefetch" href="/assets/js/120.f97dd86f.js"><link rel="prefetch" href="/assets/js/121.c5601b4a.js"><link rel="prefetch" href="/assets/js/122.2889645a.js"><link rel="prefetch" href="/assets/js/13.ebf26820.js"><link rel="prefetch" href="/assets/js/14.b92dca09.js"><link rel="prefetch" href="/assets/js/15.b3dcef45.js"><link rel="prefetch" href="/assets/js/16.50378990.js"><link rel="prefetch" href="/assets/js/17.056bba2b.js"><link rel="prefetch" href="/assets/js/18.9047bb38.js"><link rel="prefetch" href="/assets/js/19.ab53b0fe.js"><link rel="prefetch" href="/assets/js/20.fe0b8af0.js"><link rel="prefetch" href="/assets/js/21.20a702f3.js"><link rel="prefetch" href="/assets/js/22.0ee08d29.js"><link rel="prefetch" href="/assets/js/23.02039a9a.js"><link rel="prefetch" href="/assets/js/24.363d3d74.js"><link rel="prefetch" href="/assets/js/25.dd6b101e.js"><link rel="prefetch" href="/assets/js/26.20493714.js"><link rel="prefetch" href="/assets/js/27.708723e5.js"><link rel="prefetch" href="/assets/js/28.4561834a.js"><link rel="prefetch" href="/assets/js/29.15cfcf5f.js"><link rel="prefetch" href="/assets/js/3.8d39d40c.js"><link rel="prefetch" href="/assets/js/30.04ab69de.js"><link rel="prefetch" href="/assets/js/31.b11c4302.js"><link rel="prefetch" href="/assets/js/32.81ae74ee.js"><link rel="prefetch" href="/assets/js/33.ddf09cc0.js"><link rel="prefetch" href="/assets/js/34.bc3549f7.js"><link rel="prefetch" href="/assets/js/35.4a0e0ebd.js"><link rel="prefetch" href="/assets/js/36.961a1c50.js"><link rel="prefetch" href="/assets/js/37.d8658de5.js"><link rel="prefetch" href="/assets/js/38.f76063aa.js"><link rel="prefetch" href="/assets/js/39.73edd08f.js"><link rel="prefetch" href="/assets/js/4.916af9bf.js"><link rel="prefetch" href="/assets/js/40.72314bff.js"><link rel="prefetch" href="/assets/js/41.c5eb1147.js"><link rel="prefetch" href="/assets/js/42.07aeafbb.js"><link rel="prefetch" href="/assets/js/43.c4a652ba.js"><link rel="prefetch" href="/assets/js/44.91fbe22b.js"><link rel="prefetch" href="/assets/js/45.6eecf0c4.js"><link rel="prefetch" href="/assets/js/46.81b7f108.js"><link rel="prefetch" href="/assets/js/47.b4468ab3.js"><link rel="prefetch" href="/assets/js/48.df205f23.js"><link rel="prefetch" href="/assets/js/49.7df59fa3.js"><link rel="prefetch" href="/assets/js/5.4fcd540a.js"><link rel="prefetch" href="/assets/js/50.0dc2fc5b.js"><link rel="prefetch" href="/assets/js/51.1ffabe54.js"><link rel="prefetch" href="/assets/js/52.2269d284.js"><link rel="prefetch" href="/assets/js/53.81b23843.js"><link rel="prefetch" href="/assets/js/54.b3249def.js"><link rel="prefetch" href="/assets/js/55.50dcf2af.js"><link rel="prefetch" href="/assets/js/56.c399c272.js"><link rel="prefetch" href="/assets/js/57.024f1dbb.js"><link rel="prefetch" href="/assets/js/58.680c6cf4.js"><link rel="prefetch" href="/assets/js/59.2291e9d7.js"><link rel="prefetch" href="/assets/js/6.7610d1d4.js"><link rel="prefetch" href="/assets/js/60.e59a26c1.js"><link rel="prefetch" href="/assets/js/61.ff5c64e4.js"><link rel="prefetch" href="/assets/js/62.7d97062c.js"><link rel="prefetch" href="/assets/js/63.6456dc7e.js"><link rel="prefetch" href="/assets/js/64.0b055680.js"><link rel="prefetch" href="/assets/js/65.9f25601c.js"><link rel="prefetch" href="/assets/js/66.cd973084.js"><link rel="prefetch" href="/assets/js/67.9b8f9716.js"><link rel="prefetch" href="/assets/js/68.d6c1869a.js"><link rel="prefetch" href="/assets/js/69.0a3688dd.js"><link rel="prefetch" href="/assets/js/7.9ebbd956.js"><link rel="prefetch" href="/assets/js/70.22f48891.js"><link rel="prefetch" href="/assets/js/71.9eb4fda9.js"><link rel="prefetch" href="/assets/js/72.ca4fa80c.js"><link rel="prefetch" href="/assets/js/73.17d1ab37.js"><link rel="prefetch" href="/assets/js/74.139691db.js"><link rel="prefetch" href="/assets/js/75.08d0560f.js"><link rel="prefetch" href="/assets/js/76.61078c10.js"><link rel="prefetch" href="/assets/js/77.4482adcc.js"><link rel="prefetch" href="/assets/js/78.166f2c37.js"><link rel="prefetch" href="/assets/js/79.2c7d6b79.js"><link rel="prefetch" href="/assets/js/8.6f8916c2.js"><link rel="prefetch" href="/assets/js/80.80445c3f.js"><link rel="prefetch" href="/assets/js/81.14e3bae8.js"><link rel="prefetch" href="/assets/js/82.7dac687f.js"><link rel="prefetch" href="/assets/js/83.1dd2e78e.js"><link rel="prefetch" href="/assets/js/84.23b759de.js"><link rel="prefetch" href="/assets/js/85.7574f69f.js"><link rel="prefetch" href="/assets/js/86.102dac61.js"><link rel="prefetch" href="/assets/js/87.8f5d639e.js"><link rel="prefetch" href="/assets/js/88.e564cbbe.js"><link rel="prefetch" href="/assets/js/89.cd66eb78.js"><link rel="prefetch" href="/assets/js/9.baa162db.js"><link rel="prefetch" href="/assets/js/90.685c6878.js"><link rel="prefetch" href="/assets/js/91.b8d1b7ea.js"><link rel="prefetch" href="/assets/js/92.1faa374d.js"><link rel="prefetch" href="/assets/js/93.cf5ff33c.js"><link rel="prefetch" href="/assets/js/94.8a02aefb.js"><link rel="prefetch" href="/assets/js/95.1b379a5b.js"><link rel="prefetch" href="/assets/js/97.c5a6a5a1.js"><link rel="prefetch" href="/assets/js/98.9a231411.js"><link rel="prefetch" href="/assets/js/99.5b344cab.js">
    <link rel="stylesheet" href="/assets/css/0.styles.9fff9873.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">anyang的博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/go/introduction.html" class="sidebar-link">Go语言</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go教程</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go Web框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Go工具中间件</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go/middleware/go-coding-standard.html" class="sidebar-link">Golang语言编码规范</a></li><li><a href="/go/middleware/go-get命令详解.html" class="sidebar-link">go get命令详解</a></li><li><a href="/go/middleware/go-mysql.html" class="sidebar-link">Go语言操作Mysql</a></li><li><a href="/go/middleware/go-redis.html" class="sidebar-link">go-redis文档</a></li><li><a href="/go/middleware/go-swagger.html" class="sidebar-link">go-swagger安装及使用</a></li><li><a href="/go/middleware/go-viper.html" class="sidebar-link">viper操作文档</a></li><li><a href="/go/middleware/Goland的自定义快捷键配置.html" class="sidebar-link">配置Goland的自定义快捷键</a></li><li><a href="/go/middleware/Golang的安装配置golint.html" class="sidebar-link">Golang的安装配置golint</a></li><li><a href="/go/middleware/goproxy.html" class="sidebar-link">GoLand配置GOPROXY代理</a></li><li><a href="/go/middleware/gorm.html" class="sidebar-link">GORM基本操作</a></li><li><a href="/go/middleware/json-iterator.html" class="active sidebar-link">json-iterator使用</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/json-iterator.html#_1-性能对比" class="sidebar-link">1 性能对比</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/json-iterator.html#_1-1-java端性能对比" class="sidebar-link">1.1 Java端性能对比</a></li><li class="sidebar-sub-header"><a href="/go/middleware/json-iterator.html#_1-2-golang端性能对比" class="sidebar-link">1.2 Golang端性能对比</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/json-iterator.html#_2-下载依赖" class="sidebar-link">2 下载依赖</a></li><li class="sidebar-sub-header"><a href="/go/middleware/json-iterator.html#_3-简单应用示例" class="sidebar-link">3 简单应用示例</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/json-iterator.html#_3-1-序列化marshal" class="sidebar-link">3.1 序列化Marshal</a></li><li class="sidebar-sub-header"><a href="/go/middleware/json-iterator.html#_3-2-反序列化unmarshal" class="sidebar-link">3.2 反序列化Unmarshal</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/json-iterator.html#_4-jsoniter-get" class="sidebar-link">4 jsoniter.Get</a></li><li class="sidebar-sub-header"><a href="/go/middleware/json-iterator.html#_5-newdecoder" class="sidebar-link">5 NewDecoder</a></li><li class="sidebar-sub-header"><a href="/go/middleware/json-iterator.html#_6-标准库兼容配置" class="sidebar-link">6 标准库兼容配置</a></li><li class="sidebar-sub-header"><a href="/go/middleware/json-iterator.html#_7-更多资料" class="sidebar-link">7 更多资料</a></li></ul></li><li><a href="/go/middleware/package.html" class="sidebar-link">Go语言包和包管理</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="json-iterator使用"><a href="#json-iterator使用" class="header-anchor">#</a> json-iterator使用</h1> <div class="custom-block tip"><p class="custom-block-title">特点</p> <ul><li><code>json-iterator</code>是一款快且灵活的<code>JSON</code>解析器,同时提供<code>Java</code>和<code>Go</code>两个版本。</li> <li><code>json-iterator</code>是最快的<code>JSON</code>解析器。它最多能比普通的解析器快10倍之多</li> <li>独特的<code>iterator api</code>能够直接遍历<code>JSON</code> ,极致性能、0内存分配</li> <li>从<a href="https://github.com/ngs-doo/dsl-json" target="_blank" rel="noopener noreferrer">dsljson<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>和<a href="https://github.com/buger/jsonparser" target="_blank" rel="noopener noreferrer">jsonparser<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>借鉴了大量代码。</li></ul></div> <h2 id="_1-性能对比"><a href="#_1-性能对比" class="header-anchor">#</a> 1 性能对比</h2> <p>完整报告请看<a href="http://jsoniter.com/benchmark.html" target="_blank" rel="noopener noreferrer">性能评测<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>,对于<a href="http://jsoniter.com/benchmark.html#optimization-used" target="_blank" rel="noopener noreferrer">性能优化是怎么做的<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>有详尽的解释。</p> <h3 id="_1-1-java端性能对比"><a href="#_1-1-java端性能对比" class="header-anchor">#</a> 1.1 Java端性能对比</h3> <blockquote><p>主流的<code>JSON</code>解析器是非常慢的。<code>Jsoniter Java</code>版本可以比常用的<code>jackson/gson/fastjson</code>快3倍。
如果你需要处理大量的<code>JSON</code>格式的日志,你应该考虑一下用<code>dsl-json</code>或者<code>Jsoniter</code>来节约可观的成本。
根据<code>dsl-json</code>的性能评测,<code>JSON</code>格式序列化和反序列化的速度其实一点都不慢,甚至比<code>thrift/avro</code>还要快。</p></blockquote> <p><a data-fancybox="" title="protobuf-vs-jsoniter" href="/img/goImage/protobuf-vs-jsoniter.png"><img src="/img/goImage/protobuf-vs-jsoniter.png" alt="protobuf-vs-jsoniter"></a></p> <h3 id="_1-2-golang端性能对比"><a href="#_1-2-golang端性能对比" class="header-anchor">#</a> 1.2 Golang端性能对比</h3> <blockquote><p><code>Jsoniter</code>的<code>Golang</code>版本可以比标准库<code>（encoding/json）</code>快6倍之多。而且这个性能是在不使用代码生成的前提下获得的。</p></blockquote> <p><a data-fancybox="" title="go-benchmark" href="/img/goImage/go-benchmark.png"><img src="/img/goImage/go-benchmark.png" alt="go-benchmark"></a></p> <h2 id="_2-下载依赖"><a href="#_2-下载依赖" class="header-anchor">#</a> 2 下载依赖</h2> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">go</span> get github<span class="token punctuation">.</span>com<span class="token operator">/</span>json<span class="token operator">-</span>iterator<span class="token operator">/</span><span class="token keyword">go</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_3-简单应用示例"><a href="#_3-简单应用示例" class="header-anchor">#</a> 3 简单应用示例</h2> <h3 id="_3-1-序列化marshal"><a href="#_3-1-序列化marshal" class="header-anchor">#</a> 3.1 序列化Marshal</h3> <blockquote><p><code>jsoniter</code>常用序列化函数如下</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 直接把结构体转化成字符串</span>
<span class="token function">MarshalToString</span><span class="token punctuation">(</span>v <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
<span class="token comment">//把结构体转化成json,兼容go标准库encoding/json的序列化方法,返回一个字节切片和错误</span>
<span class="token function">Marshal</span><span class="token punctuation">(</span>v <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
<span class="token comment">//转化成字节切片,第一个参数是结构体对象，第二个参数是前缀字符串必须为&quot;&quot;,第三个参数为缩进表示，只能是空格</span>
<span class="token function">MarshalIndent</span><span class="token punctuation">(</span>v <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> prefix<span class="token punctuation">,</span> indent <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;github.com/json-iterator/go&quot;</span>
<span class="token punctuation">)</span>

<span class="token comment">// Student 测试转换json的结构体</span>
<span class="token keyword">type</span> Student <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name    <span class="token builtin">string</span>   <span class="token string">`json:&quot;name&quot;`</span>
	Age     <span class="token builtin">int</span>      <span class="token string">`json:&quot;age&quot;`</span>
	Address <span class="token builtin">string</span>   <span class="token string">`json:&quot;address&quot;`</span>
	Hobby   <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span> <span class="token string">`json:&quot;hobby&quot;`</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	stu <span class="token operator">:=</span> Student<span class="token punctuation">{</span>
		Name<span class="token punctuation">:</span>    <span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span>
		Age<span class="token punctuation">:</span>     <span class="token number">18</span><span class="token punctuation">,</span>
		Address<span class="token punctuation">:</span> <span class="token string">&quot;chengdu&quot;</span><span class="token punctuation">,</span>
		Hobby<span class="token punctuation">:</span>   <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;football&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;swimming&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;travel&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;sing&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// Marshal(v interface{}) ([]byte, error)</span>
	<span class="token comment">//把结构体转化成json,兼容go标准库encoding/json的序列化方法,返回一个字节切片和错误</span>
	b<span class="token punctuation">,</span> err <span class="token operator">:=</span> jsoniter<span class="token punctuation">.</span><span class="token function">Marshal</span><span class="token punctuation">(</span>stu<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;transformation error: &quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//输出转化后的字符串</span>
	<span class="token comment">//{&quot;name&quot;:&quot;张三&quot;,&quot;age&quot;:18,&quot;address&quot;:&quot;chengdu&quot;,&quot;hobby&quot;:[&quot;football&quot;,&quot;swimming&quot;,&quot;travel&quot;,&quot;sing&quot;]}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">//直接把结构体转化成字符串MarshalToString,这里错误判断省略</span>
	str<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> jsoniter<span class="token punctuation">.</span><span class="token function">MarshalToString</span><span class="token punctuation">(</span>stu<span class="token punctuation">)</span>
    <span class="token comment">//{&quot;name&quot;:&quot;张三&quot;,&quot;age&quot;:18,&quot;address&quot;:&quot;chengdu&quot;,&quot;hobby&quot;:[&quot;football&quot;,&quot;swimming&quot;,&quot;travel&quot;,&quot;sing&quot;]}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span>
	<span class="token comment">//转化成字节切片,第一个参数是结构体对象，第二个参数是前缀字符串必须为&quot;&quot;,第三个参数为缩进表示，只能是空格</span>
	b<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">=</span> jsoniter<span class="token punctuation">.</span><span class="token function">MarshalIndent</span><span class="token punctuation">(</span>stu<span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">)</span>
    <span class="token comment">//输出带格式的字符串</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>输出结果<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>
调用了Calc的初始化方法
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">:</span><span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">:</span><span class="token number">18</span><span class="token punctuation">,</span><span class="token string">&quot;address&quot;</span><span class="token punctuation">:</span><span class="token string">&quot;chengdu&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;hobby&quot;</span><span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token string">&quot;football&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;swimming&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;travel&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;sing&quot;</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">:</span><span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">:</span><span class="token number">18</span><span class="token punctuation">,</span><span class="token string">&quot;address&quot;</span><span class="token punctuation">:</span><span class="token string">&quot;chengdu&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;hobby&quot;</span><span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token string">&quot;football&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;swimming&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;travel&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;sing&quot;</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span>
 <span class="token string">&quot;name&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span>
 <span class="token string">&quot;age&quot;</span><span class="token punctuation">:</span> <span class="token number">18</span><span class="token punctuation">,</span>
 <span class="token string">&quot;address&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;chengdu&quot;</span><span class="token punctuation">,</span>
 <span class="token string">&quot;hobby&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>
  <span class="token string">&quot;football&quot;</span><span class="token punctuation">,</span>
  <span class="token string">&quot;swimming&quot;</span><span class="token punctuation">,</span>
  <span class="token string">&quot;travel&quot;</span><span class="token punctuation">,</span>
  <span class="token string">&quot;sing&quot;</span>
 <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br></div></div><h3 id="_3-2-反序列化unmarshal"><a href="#_3-2-反序列化unmarshal" class="header-anchor">#</a> 3.2 反序列化Unmarshal</h3> <blockquote><p><code>jsoniter</code>常用反序列化函数如下</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//把字节切片的JSON格式转换成对应的结构体</span>
<span class="token function">Unmarshal</span><span class="token punctuation">(</span>data <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> v <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token builtin">error</span> 
<span class="token comment">//把字符串的JSON格式转换成对应的结构体</span>
<span class="token function">UnmarshalFromString</span><span class="token punctuation">(</span>str <span class="token builtin">string</span><span class="token punctuation">,</span> v <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token builtin">error</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;github.com/json-iterator/go&quot;</span>
<span class="token punctuation">)</span>

<span class="token comment">// Student 测试转换json的结构体</span>
<span class="token keyword">type</span> Student <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name    <span class="token builtin">string</span>   <span class="token string">`json:&quot;name&quot;`</span>
	Age     <span class="token builtin">int</span>      <span class="token string">`json:&quot;age&quot;`</span>
	Address <span class="token builtin">string</span>   <span class="token string">`json:&quot;address&quot;`</span>
	Hobby   <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span> <span class="token string">`json:&quot;hobby&quot;`</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//反序列化给一个结构体</span>
	<span class="token keyword">var</span> stu Student
	<span class="token keyword">var</span> jsonBlob <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">`
        {&quot;name&quot;: &quot;张三&quot;, &quot;Age&quot;: 12,&quot;Address&quot;:&quot;chengdu&quot;,&quot;Hobby&quot;:[&quot;football&quot;, &quot;swimming&quot;, &quot;travel&quot;, &quot;sing&quot;]}
    `</span><span class="token punctuation">)</span>
	<span class="token comment">//根据字节切片转换成结构体。注意这里传入的是结构体地址</span>
	err <span class="token operator">:=</span> jsoniter<span class="token punctuation">.</span><span class="token function">Unmarshal</span><span class="token punctuation">(</span>jsonBlob<span class="token punctuation">,</span> <span class="token operator">&amp;</span>stu<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;unmarshal error: &quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;根据字节切片转换的结构体数据：%+v\n&quot;</span><span class="token punctuation">,</span> stu<span class="token punctuation">)</span>

	<span class="token comment">//根据字节切片转换成结构体</span>
	<span class="token keyword">var</span> students <span class="token punctuation">[</span><span class="token punctuation">]</span>Student
	<span class="token keyword">var</span> jsonSlice <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">`[
        {&quot;name&quot;: &quot;张三&quot;, &quot;Age&quot;: 12,&quot;Address&quot;:&quot;chengdu&quot;,&quot;Hobby&quot;:[&quot;football&quot;, &quot;swimming&quot;, &quot;travel&quot;, &quot;sing&quot;]},
        {&quot;name&quot;: &quot;李四&quot;, &quot;Age&quot;: 28,&quot;Address&quot;:&quot;sichuan&quot;,&quot;Hobby&quot;:[&quot;dance&quot;, &quot;music&quot;]}
   ]`</span><span class="token punctuation">)</span>
	<span class="token comment">//根据字节切片转换成结构体。把多个结构体json数据反序列化给结构体切片</span>
	err <span class="token operator">=</span> jsoniter<span class="token punctuation">.</span><span class="token function">Unmarshal</span><span class="token punctuation">(</span>jsonSlice<span class="token punctuation">,</span> <span class="token operator">&amp;</span>students<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;unmarshal error: &quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;根据字节切片转换的结构体数据：%+v\n&quot;</span><span class="token punctuation">,</span> students<span class="token punctuation">)</span>
	<span class="token comment">//根据</span>
	<span class="token keyword">var</span> stu1 Student
	stuStr <span class="token operator">:=</span> <span class="token string">`{&quot;name&quot;: &quot;张三&quot;, &quot;Age&quot;: 12,&quot;Address&quot;:&quot;chengdu&quot;,&quot;Hobby&quot;:[&quot;football&quot;, &quot;swimming&quot;, &quot;travel&quot;, &quot;sing&quot;]`</span>
	<span class="token comment">//根据字符串转换成结构体。注意这里传入的是结构体地址</span>
	jsoniter<span class="token punctuation">.</span><span class="token function">UnmarshalFromString</span><span class="token punctuation">(</span>stuStr<span class="token punctuation">,</span> <span class="token operator">&amp;</span>stu1<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;根据字符串转换的结构体数据：%+v\n&quot;</span><span class="token punctuation">,</span> stu1<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>输出结果<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
调用了Calc的初始化方法
根据字节切片转换的结构体数据：<span class="token punctuation">{</span>Name<span class="token punctuation">:</span>张三 Age<span class="token punctuation">:</span><span class="token number">12</span> Address<span class="token punctuation">:</span>chengdu Hobby<span class="token punctuation">:</span><span class="token punctuation">[</span>football swimming travel sing<span class="token punctuation">]</span><span class="token punctuation">}</span>
根据字节切片转换的结构体数据：<span class="token punctuation">[</span><span class="token punctuation">{</span>Name<span class="token punctuation">:</span>张三 Age<span class="token punctuation">:</span><span class="token number">12</span> Address<span class="token punctuation">:</span>chengdu Hobby<span class="token punctuation">:</span><span class="token punctuation">[</span>football swimming travel sing<span class="token punctuation">]</span><span class="token punctuation">}</span> <span class="token punctuation">{</span>Name<span class="token punctuation">:</span>李四 Age<span class="token punctuation">:</span><span class="token number">28</span> Address<span class="token punctuation">:</span>sichuan Hobby<span class="token punctuation">:</span><span class="token punctuation">[</span>dance music<span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
根据字符串转换的结构体数据：<span class="token punctuation">{</span>Name<span class="token punctuation">:</span>张三 Age<span class="token punctuation">:</span><span class="token number">12</span> Address<span class="token punctuation">:</span>chengdu Hobby<span class="token punctuation">:</span><span class="token punctuation">[</span>football swimming travel sing<span class="token punctuation">]</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br></div></div><h2 id="_4-jsoniter-get"><a href="#_4-jsoniter-get" class="header-anchor">#</a> 4 jsoniter.Get</h2> <h4 id="获取深层嵌套json结构的值的快速方法"><a href="#获取深层嵌套json结构的值的快速方法" class="header-anchor">#</a> 获取深层嵌套JSON结构的值的快速方法</h4> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//惰性json实现保持[]byte并延迟解析，把 json 解析为 Any 对象</span>
<span class="token function">Get</span><span class="token punctuation">(</span>data <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> path <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> Any
<span class="token comment">//Any通用对象表示</span>
<span class="token comment">//最简单的json实现持有字节切片,并延迟解析</span>
<span class="token keyword">type</span> Any <span class="token keyword">interface</span> <span class="token punctuation">{</span>
	<span class="token function">LastError</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">error</span>
	<span class="token function">ValueType</span><span class="token punctuation">(</span><span class="token punctuation">)</span> ValueType
	<span class="token function">MustBeValid</span><span class="token punctuation">(</span><span class="token punctuation">)</span> Any
	<span class="token function">ToBool</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>
	<span class="token function">ToInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int</span>
	<span class="token function">ToInt32</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int32</span>
	<span class="token function">ToInt64</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int64</span>
	<span class="token function">ToUint</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">uint</span>
	<span class="token function">ToUint32</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">uint32</span>
	<span class="token function">ToUint64</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">uint64</span>
	<span class="token function">ToFloat32</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">float32</span>
	<span class="token function">ToFloat64</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">float64</span>
	<span class="token function">ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>
	<span class="token function">ToVal</span><span class="token punctuation">(</span>val <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
	<span class="token function">Get</span><span class="token punctuation">(</span>path <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> Any
	<span class="token function">Size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int</span>
	<span class="token function">Keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span>
	<span class="token function">GetInterface</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
	<span class="token function">WriteTo</span><span class="token punctuation">(</span>stream <span class="token operator">*</span>Stream<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	jsoniter <span class="token string">&quot;github.com/json-iterator/go&quot;</span>
<span class="token punctuation">)</span>

<span class="token comment">// Student 测试转换json的结构体</span>
<span class="token keyword">type</span> Student <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name    <span class="token builtin">string</span>   <span class="token string">`json:&quot;name&quot;`</span>
	Age     <span class="token builtin">int</span>      <span class="token string">`json:&quot;age&quot;`</span>
	Address <span class="token builtin">string</span>   <span class="token string">`json:&quot;address&quot;`</span>
	Hobby   <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span> <span class="token string">`json:&quot;hobby&quot;`</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">var</span> jsonBlob <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">`
        {&quot;name&quot;: &quot;张三&quot;, &quot;Age&quot;: 12,&quot;Address&quot;:&quot;chengdu&quot;,&quot;Hobby&quot;:[&quot;football&quot;, &quot;swimming&quot;, &quot;travel&quot;, &quot;sing&quot;]}
    `</span><span class="token punctuation">)</span>
	<span class="token comment">//获取深层嵌套JSON结构的值的快速方法</span>
	<span class="token comment">//返回一个数组指针</span>
	hobby <span class="token operator">:=</span> jsoniter<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span>jsonBlob<span class="token punctuation">,</span> <span class="token string">&quot;Hobby&quot;</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%T\n&quot;</span><span class="token punctuation">,</span>hobby<span class="token punctuation">)</span><span class="token comment">//*jsoniter.arrayLazyAny</span>
	<span class="token comment">//获取这个结构体Hobby元素下面的第二个元素值</span>
	str <span class="token operator">:=</span> jsoniter<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span>jsonBlob<span class="token punctuation">,</span> <span class="token string">&quot;Hobby&quot;</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span> <span class="token comment">//swimming</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h2 id="_5-newdecoder"><a href="#_5-newdecoder" class="header-anchor">#</a> 5 NewDecoder</h2> <blockquote><p>通过流的方式操作<code>json</code>,适用于大文件<code>json</code></p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//NewDecoder适用于json/stream NewDecoder API。</span>
<span class="token comment">//new decoder返回从r读取的新解码器。</span>
<span class="token comment">//不是json/编码解码器，而是返回一个解码器</span>
<span class="token comment">//更多信息请参考https://godoc.org/encoding/json#NewDecoder</span>
<span class="token function">NewDecoder</span><span class="token punctuation">(</span>reader io<span class="token punctuation">.</span>Reader<span class="token punctuation">)</span> <span class="token operator">*</span>Decoder 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;strings&quot;</span>

	<span class="token string">&quot;github.com/json-iterator/go&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	json <span class="token operator">:=</span> jsoniter<span class="token punctuation">.</span>ConfigCompatibleWithStandardLibrary
	reader <span class="token operator">:=</span> strings<span class="token punctuation">.</span><span class="token function">NewReader</span><span class="token punctuation">(</span><span class="token string">`{&quot;branch&quot;:&quot;beta&quot;,&quot;change_log&quot;:&quot;add the rows{10}&quot;,&quot;channel&quot;:&quot;fros&quot;,&quot;create_time&quot;:&quot;2017-06-13 16:39:08&quot;,&quot;firmware_list&quot;:&quot;&quot;,&quot;md5&quot;:&quot;80dee2bf7305bcf179582088e29fd7b9&quot;,&quot;note&quot;:{&quot;CoreServices&quot;:{&quot;md5&quot;:&quot;d26975c0a8c7369f70ed699f2855cc2e&quot;,&quot;package_name&quot;:&quot;CoreServices&quot;,&quot;version_code&quot;:&quot;76&quot;,&quot;version_name&quot;:&quot;1.0.76&quot;},&quot;FrDaemon&quot;:{&quot;md5&quot;:&quot;6b1f0626673200bc2157422cd2103f5d&quot;,&quot;package_name&quot;:&quot;FrDaemon&quot;,&quot;version_code&quot;:&quot;390&quot;,&quot;version_name&quot;:&quot;1.0.390&quot;},&quot;FrGallery&quot;:{&quot;md5&quot;:&quot;90d767f0f31bcd3c1d27281ec979ba65&quot;,&quot;package_name&quot;:&quot;FrGallery&quot;,&quot;version_code&quot;:&quot;349&quot;,&quot;version_name&quot;:&quot;1.0.349&quot;},&quot;FrLocal&quot;:{&quot;md5&quot;:&quot;f15a215b2c070a80a01f07bde4f219eb&quot;,&quot;package_name&quot;:&quot;FrLocal&quot;,&quot;version_code&quot;:&quot;791&quot;,&quot;version_name&quot;:&quot;1.0.791&quot;}},&quot;pack_region_urls&quot;:{&quot;CN&quot;:&quot;https://s3.cn-north-1.amazonaws.com.cn/xxx-os/ttt_xxx_android_1.5.3.344.393.zip&quot;,&quot;default&quot;:&quot;http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip&quot;,&quot;local&quot;:&quot;http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip&quot;},&quot;pack_version&quot;:&quot;1.5.3.344.393&quot;,&quot;pack_version_code&quot;:393,&quot;region&quot;:&quot;all&quot;,&quot;release_flag&quot;:0,&quot;revision&quot;:62,&quot;size&quot;:38966875,&quot;status&quot;:3}`</span><span class="token punctuation">)</span>
	decoder <span class="token operator">:=</span> json<span class="token punctuation">.</span><span class="token function">NewDecoder</span><span class="token punctuation">(</span>reader<span class="token punctuation">)</span>
	params <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
	err <span class="token operator">:=</span> decoder<span class="token punctuation">.</span><span class="token function">Decode</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>params<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token comment">//map[firmware_list: note:map[CoreServices:map[package_name:CoreServices version_code:76 version_name:1.0.76 md5:d26975c0a8c7369f70ed699f2855cc2e] FrDaemon:map[md5:6b1f0626673200bc2157422cd2103f5d package_name:FrDaemon version_code:390 version_name:1.0.390] FrGallery:map[version_code:349 version_name:1.0.349 md5:90d767f0f31bcd3c1d27281ec979ba65 package_name:FrGallery] FrLocal:map[version_name:1.0.791 md5:f15a215b2c070a80a01f07bde4f219eb package_name:FrLocal version_code:791]] pack_version:1.5.3.344.393 pack_version_code:393 status:3 channel:fros pack_region_urls:map[CN:https://s3.cn-north-1.amazonaws.com.cn/xxx-os/ttt_xxx_android_1.5.3.344.393.zip default:http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip local:http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip] release_flag:0 size:3.8966875e+07 md5:80dee2bf7305bcf179582088e29fd7b9 region:all revision:62 change_log:add the rows{10} create_time:2017-06-13 16:39:08 branch:beta]</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%+v\n&quot;</span><span class="token punctuation">,</span> params<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h2 id="_6-标准库兼容配置"><a href="#_6-标准库兼容配置" class="header-anchor">#</a> 6 标准库兼容配置</h2> <blockquote><p><code>ConfigCompatibleWithStandardLibrary</code>尝试与标准库行为100%兼容配置</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;github.com/json-iterator/go&quot;</span>
<span class="token punctuation">)</span>

<span class="token comment">// Student 测试转换json的结构体</span>
<span class="token keyword">type</span> Student <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name    <span class="token builtin">string</span>   <span class="token string">`json:&quot;name&quot;`</span>
	Age     <span class="token builtin">int</span>      <span class="token string">`json:&quot;age&quot;`</span>
	Address <span class="token builtin">string</span>   <span class="token string">`json:&quot;address&quot;`</span>
	Hobby   <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span> <span class="token string">`json:&quot;hobby&quot;`</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	stu <span class="token operator">:=</span> Student<span class="token punctuation">{</span>
		Name<span class="token punctuation">:</span>    <span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span>
		Age<span class="token punctuation">:</span>     <span class="token number">18</span><span class="token punctuation">,</span>
		Address<span class="token punctuation">:</span> <span class="token string">&quot;chengdu&quot;</span><span class="token punctuation">,</span>
		Hobby<span class="token punctuation">:</span>   <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;football&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;swimming&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;travel&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;sing&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//返回与标准库行为100%兼容的配置对象，返回encoding/json的操作对象</span>
	<span class="token keyword">var</span> json <span class="token operator">=</span> jsoniter<span class="token punctuation">.</span>ConfigCompatibleWithStandardLibrary
	b<span class="token punctuation">,</span> err <span class="token operator">:=</span> json<span class="token punctuation">.</span><span class="token function">Marshal</span><span class="token punctuation">(</span>stu<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;transformation error: &quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><h2 id="_7-更多资料"><a href="#_7-更多资料" class="header-anchor">#</a> 7 更多资料</h2> <p><a href="https://github.com/json-iterator/go" target="_blank" rel="noopener noreferrer">jsoniter源码<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <p><a href="http://jsoniter.com/index.cn.html" target="_blank" rel="noopener noreferrer">jsoniter中文官网<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <p><a href="https://godoc.org/github.com/json-iterator/go" target="_blank" rel="noopener noreferrer">jsoniter API文档<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/go/middleware/gorm.html" class="prev">
        GORM基本操作
      </a></span> <span class="next"><a href="/go/middleware/package.html">
        Go语言包和包管理
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.1bc80adb.js" defer></script><script src="/assets/js/2.7c0608ab.js" defer></script><script src="/assets/js/96.9fd67185.js" defer></script>
  </body>
</html>
